package main

import (
	"fmt"
	"time"
)

func main3() {
	c1 := make(chan int, 5)
	fmt.Printf("c1: %d, %d, %v\n", len(c1), cap(c1), c1)
	c1 <- 111
	c1 <- 222
	c1 <- 333
	fmt.Println(<-c1, "###")
	close(c1)
	for v := range c1 {
		fmt.Println(v, "~~~")
	}
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

}

func main4() {
	c1 := make(chan int)
	fmt.Printf("c1: %d, %d, %v\n", len(c1), cap(c1), c1)

	go func() {
		count := 1
		for {
			time.Sleep(3 * time.Second)
			c1 <- count
			count++
		}
	}()

	for v := range c1 {
		fmt.Println(v, "~~~")
	}
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

}

func main5() {
	c1 := make(chan int)
	fmt.Printf("c1: %d, %d, %v\n", len(c1), cap(c1), c1)

	go func() {
		defer close(c1)
		count := 1
		for i := 0; i < 5; i++ {
			time.Sleep(3 * time.Second)
			c1 <- count
			count++
		}
	}()

	for v := range c1 {
		fmt.Println(v, "~~~")
	}
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

}

func main() {
	// main3()
	// main4()
	main5()
}
